<template>
    <div class="box">
        <van-nav-bar title="登录" />
        <img style="margin: 0px 35%;" src="../../assets/_.jpg" alt="" srcset="">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="手机号码或邮箱"
                    :rules="[{pattern:telpattern,required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" :type="checked?'text':'password'" name="password" label="密码" placeholder="登录密码"
                    :rules="[{pattern:pwdpattern,required: true, message: '请填写密码' }]">
                    <template #button>
                        <van-switch v-model="checked" />
                    </template>
                </van-field>
            </van-cell-group>
            <p style="display: flex;float: right;color: #2988fa;font-weight: 600;">忘记密码</p>
            <br>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
            <p style="color: #2988fa;text-align: center;font-weight: 600;">注册</p>
            <p style="margin: 30px 20px;"><img src="../../assets/1 (2).jpg" alt=""></p>
        </van-form>

    </div>
</template>

<script setup>
import { ref } from 'vue';
import { showToast } from 'vant';
import { loginApi } from '@/api/api';
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
const checked = ref(true);
const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value,password.value).then(res => {
        if (res.data.code == 0) {
          showToast('登录成功');
          localStorage.setItem('token',res.data.data.token)
          localStorage.setItem('username',res.data.data.username)
          router.push('/home/my')
        }
        else
        {
           showToast(res.data.msg);
        }
    })
};

  const telpattern = /^1[3-9]\d{9}$/;
  const pwdpattern = /^\w{8,}$/;
</script>

<style lang="scss" scoped>
.box {
    background-color: #fbfafa;
}
</style>